﻿#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>

void arrrightmove()
{
	int n, k;
	printf("请输入数组长度n和右移位数k,以空格分开\n");
	scanf("%d %d", &n, &k);
	k %= n;
	int* arr = (int*)malloc(n * sizeof(int));
	printf("请输入%d个整数", n);
	for (int i = 0; i < n; i++)
	{
		scanf("%d", &arr[i]);
	}
	int* result = (int*)malloc(n * sizeof(int));
	for (int i = 0; i < n; i++)
	{
		result[i] = arr[(i - k + n) % n];
	}
	for (int i = 0; i < n; i++)
	{
		printf("%d ", result[i]);
	}
	free(arr);
	free(result);
	printf("\n");
}
void findmax()
{
	int n;
	printf("请输入数组长度n: ");
	scanf("%d", &n);
	int* arr = (int*)malloc(n * sizeof(int));
	printf("请输入%d个整数: ", n);
	for (int i = 0; i < n; i++)
	{
		scanf("%d", &arr[i]);
	}
	for (int i = 0; i < n; i++)
	{
		int left = (i == 0) ? -1 : arr[i - 1], right = (i == n - 1) ? -1 : arr[i + 1];
		if (arr[i] > left && arr[i] > right)
		{
			printf("%d\n", i);

			break;
		}
	}
	free(arr);
	printf("\n");
}
void findmin() {
	int n;
	printf("请输入数组长度n: ");
	scanf("%d", &n);
	int* arr = (int*)malloc(n * sizeof(int));
	printf("请输入%d个整数: ", n);
	for (int i = 0; i < n; i++) {
		scanf("%d", &arr[i]);
	}
	int left = 0, right = n - 1;
	while (left < right) {
		int mid =( left -right)/ 2;

		if (arr[mid] > arr[right]) 
		{
			left = mid + 1;
		}else if (arr[mid] < arr[right]) 
		{
			right = mid;
		}
		else {
			right--;
		}
	}
	printf("%d\n", arr[left]);
	free(arr);
}

int main()
{
	char choice;
	while (1)
	{
		printf("功能选择：\n1.数组循环右移\n2.求数组峰值索引\n3.旋转数组的最小数字\n4.退出\n请输入选择\n");
		scanf("%c", &choice);
		switch (choice)
		{
		case '1':arrrightmove(); break;
		case '2':findmax(); break;
		case '3':findmin(); break;
		case '4':return 0; break;
		default:printf("预期外的选择！"); break;
		}
		printf("\n");
		getchar();
	}
}



